home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / mips-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-02  |  32.7 KB  |  1,152 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * The port to MIPS-based machines was mostly done
  22.  * by Per Bothner, bothner@cs.wisc.edu.
  23.  * The inspiration for the heuristics to deal with missing
  24.  * symbol tables, as well as many of the specific ideas for doing so,
  25.  * comes from Alessando Forin (Alessando.Forin@spice.cs.cmu.edu).
  26.  */
  27. #include "defs.h"
  28. #include "param.h"
  29. #include "frame.h"
  30. #include "inferior.h"
  31. #include "value.h"
  32.  
  33. #ifdef USG
  34. #include <sys/types.h>
  35. #endif
  36.  
  37. #include <stdio.h>
  38. #include <sys/param.h>
  39. #include <sys/ptrace.h>
  40. #include <sys/dir.h>
  41. #include <signal.h>
  42. #include <sys/ioctl.h>
  43. /* #include <fcntl.h>  Can we live without this?  */
  44.  
  45. #ifdef COFF_ENCAPSULATE
  46. #include "a.out.encap.h"
  47. #else
  48. #include <a.out.h>
  49. #endif
  50. #ifndef N_SET_MAGIC
  51. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  52. #endif
  53. #define VM_MIN_ADDRESS (unsigned)0x400000
  54. #include <sys/user.h>        /* After a.out.h  */
  55. #include <sys/file.h>
  56. #include <sys/stat.h>
  57. #include <machine/reg.h>
  58.  
  59. #ifdef COFF_FORMAT
  60. #include "symtab.h"
  61.  
  62. #ifdef USG
  63. #include <sys/types.h>
  64. #include <fcntl.h>
  65. #endif
  66.  
  67. #include <obstack.h>
  68. /*#include <sys/param.h>*/
  69. /*#include <sys/file.h>*/
  70.  
  71. extern void close ();
  72.  
  73. extern PDR *proc_desc_table;
  74. extern long proc_desc_length;
  75.  
  76. struct linked_proc_info
  77. {
  78.   mips_proc_info info;
  79.   struct linked_proc_info *next;
  80. } * linked_proc_desc_table = NULL;
  81.  
  82. extern int errno;
  83.  
  84. /* This function simply calls ptrace with the given arguments.  
  85.    It exists so that all calls to ptrace are isolated in this 
  86.    machine-dependent file. */
  87. int
  88. call_ptrace (request, pid, arg3, arg4)
  89.      int request, pid, arg3, arg4;
  90. {
  91.   return ptrace (request, pid, arg3, arg4);
  92. }
  93.  
  94. kill_inferior ()
  95. {
  96.   if (remote_debugging)
  97.     return;
  98.   if (inferior_pid == 0)
  99.     return;
  100.   ptrace (8, inferior_pid, 0, 0);
  101.   wait (0);
  102.   inferior_died ();
  103. }
  104.  
  105. /* This is used when GDB is exiting.  It gives less chance of error.*/
  106.  
  107. kill_inferior_fast ()
  108. {
  109.   if (remote_debugging)
  110.     return;
  111.   if (inferior_pid == 0)
  112.     return;
  113.   ptrace (8, inferior_pid, 0, 0);
  114.   wait (0);
  115. }
  116.  
  117. /* Resume execution of the inferior process.
  118.    If STEP is nonzero, single-step it.
  119.    If SIGNAL is nonzero, give it that signal.  */
  120.  
  121. void
  122. resume (step, signal)
  123.      int step;
  124.      int signal;
  125. {
  126.   errno = 0;
  127.   if (remote_debugging)
  128.     remote_resume (step, signal);
  129.   else
  130.     {
  131.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  132.       if (errno)
  133.     perror_with_name ("ptrace");
  134.     }
  135. }
  136.  
  137. static char register_ptrace_map[NUM_REGS] = {
  138.     /* general registers */
  139.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  140.     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  141.     /* floating-point registers */
  142.     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  143.     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  144.     /* special registers */
  145.     PC, /* 96, pc */
  146.     CAUSE, /* 97,cause */
  147.     MMHI, /* 98,mdhi */
  148.     MMLO, /* 99,mdlow */
  149.     FPC_CSR,/* 100,fpc_csr */
  150.     FPC_EIR,/* 101,fpc_eir */
  151.     -1 /* fp */
  152.   };
  153.     
  154. void
  155. fetch_inferior_registers ()
  156. {
  157.   register int regno;
  158.   register unsigned int regaddr;
  159.   char buf[MAX_REGISTER_RAW_SIZE];
  160.   register int i;
  161.  
  162.   struct user u;
  163.  
  164.   for (regno = 0; regno < NUM_REGS; regno++)
  165.     {
  166.       regaddr = register_ptrace_map[regno];
  167.       if (regaddr < 0) continue;
  168.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  169.      {
  170.        *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  171.        regaddr++;
  172.      }
  173.       supply_register (regno, buf);
  174.     }
  175.   /* for now, make FP = SP */
  176.   *(long*)buf = read_register(SP_REGNUM);
  177.   supply_register(FP_REGNUM, buf);
  178. }
  179.  
  180. /* Store our register values back into the inferior.
  181.    If REGNO is -1, do this for all registers.
  182.    Otherwise, REGNO specifies which register (so we can save time).  */
  183.  
  184. store_inferior_registers (regno)
  185.      int regno;
  186. {
  187.   register unsigned int regaddr;
  188.   char buf[80];
  189.   extern char registers[];
  190.   unsigned int i;
  191.  
  192.   if (regno > FP_REGNUM)
  193.     return;
  194.   if (regno >= 0)
  195.     {
  196.       regaddr = register_ptrace_map[regno];
  197.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  198.     {
  199.       errno = 0;
  200.       ptrace (PT_WRITE_U, inferior_pid, regaddr,
  201.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  202.       if (errno != 0 && regno != CAUSE_REGNUM)
  203.         {
  204.           sprintf (buf, "writing register number %d(ptrace#%d)",
  205.                regno, regaddr);
  206.           perror_with_name (buf);
  207.         }
  208.       regaddr++;
  209.     }
  210.       return;
  211.     }
  212.  
  213.  if (read_register (0) != 0)
  214.    {
  215.      int zero = 0;
  216.      error ("Cannot change register 0.");
  217.      supply_register (regno, &zero);
  218.    }
  219.   for (regno = 1; regno < FP_REGNUM; regno++)
  220.     {
  221.       regaddr = register_ptrace_map[regno];
  222.       if (regaddr < 0) continue;
  223.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  224.     {
  225.       errno = 0;
  226.       ptrace (PT_WRITE_U, inferior_pid, regaddr,
  227.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  228.       if (errno != 0)
  229.         {
  230.           sprintf (buf, "writing all regs, number %d(ptrace#%d)",
  231.                regno, regaddr);
  232.           perror_with_name (buf);
  233.         }
  234.       regaddr++;
  235.     }
  236.     }
  237. }
  238.  
  239. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  240.    to debugger memory starting at MYADDR. 
  241.    On failure (cannot read from inferior, usually because address is out
  242.    of bounds) returns the value of errno. */
  243.  
  244. int
  245. read_inferior_memory (memaddr, myaddr, len)
  246.      CORE_ADDR memaddr;
  247.      char *myaddr;
  248.      int len;
  249. {
  250.   register int i;
  251.   /* Round starting address down to longword boundary.  */
  252.   register CORE_ADDR addr = memaddr & - sizeof (int);
  253.   /* Round ending address up; get number of longwords that makes.  */
  254.   register int count
  255.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  256.   /* Allocate buffer of that many longwords.  */
  257.   register int *buffer = (int *) alloca (count * sizeof (int));
  258.   extern int errno;
  259.  
  260.   /* Read all the longwords */
  261.   for (i = 0; i < count; i++, addr += sizeof (int))
  262.     {
  263.       errno = 0;
  264. #if 0
  265. This is now done by read_memory, because when this function did it,
  266.   reading a byte or short int hardware port read whole longs, causing
  267.   serious side effects
  268.   such as bus errors and unexpected hardware operation.  This would
  269.   also be a problem with ptrace if the inferior process could read
  270.   or write hardware registers, but that's not usually the case.
  271.       if (remote_debugging)
  272.     buffer[i] = remote_fetch_word (addr);
  273.       else
  274. #endif
  275.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  276.       if (errno)
  277.     return errno;
  278.     }
  279.  
  280.   /* Copy appropriate bytes out of the buffer.  */
  281.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  282.   return 0;
  283. }
  284.  
  285. /* Copy LEN bytes of data from debugger memory at MYADDR
  286.    to inferior's memory at MEMADDR.
  287.    On failure (cannot write the inferior)
  288.    returns the value of errno.  */
  289.  
  290. int
  291. write_inferior_memory (memaddr, myaddr, len)
  292.      CORE_ADDR memaddr;
  293.      char *myaddr;
  294.      int len;
  295. {
  296.   register int i;
  297.   /* Round starting address down to longword boundary.  */
  298.   register CORE_ADDR addr = memaddr & - sizeof (int);
  299.   /* Round ending address up; get number of longwords that makes.  */
  300.   register int count
  301.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  302.   /* Allocate buffer of that many longwords.  */
  303.   register int *buffer = (int *) alloca (count * sizeof (int));
  304.   extern int errno;
  305.  
  306.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  307.  
  308.   if (remote_debugging)
  309.     buffer[0] = remote_fetch_word (addr);
  310.   else
  311.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  312.  
  313.   if (count > 1)
  314.     {
  315.       if (remote_debugging)
  316.     buffer[count - 1]
  317.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  318.       else
  319.     buffer[count - 1]
  320.       = ptrace (1, inferior_pid,
  321.             addr + (count - 1) * sizeof (int), 0);
  322.     }
  323.  
  324.   /* Copy data to be written over corresponding part of buffer */
  325.  
  326.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  327.  
  328.   /* Write the entire buffer.  */
  329.  
  330.   for (i = 0; i < count; i++, addr += sizeof (int))
  331.     {
  332.       errno = 0;
  333.       if (remote_debugging)
  334.     remote_store_word (addr, buffer[i]);
  335.       else
  336.     ptrace (4, inferior_pid, addr, buffer[i]);
  337.       if (errno)
  338.     return errno;
  339.     }
  340.  
  341.   return 0;
  342. }
  343.  
  344. /* Work with core dump and executable files, for GDB. 
  345.    This code would be in core.c if it weren't machine-dependent. */
  346.  
  347. #ifndef N_TXTADDR
  348. #define N_TXTADDR(hdr) 0
  349. #endif /* no N_TXTADDR */
  350.  
  351. #ifndef N_DATADDR
  352. #define N_DATADDR(hdr) hdr.a_text
  353. #endif /* no N_DATADDR */
  354.  
  355. /* Make COFF and non-COFF names for things a little more compatible
  356.    to reduce conditionals later.  */
  357.  
  358. #ifdef COFF_FORMAT
  359. #define a_magic magic
  360. #endif
  361.  
  362. #ifndef COFF_FORMAT
  363. #ifndef AOUTHDR
  364. #define AOUTHDR struct exec
  365. #endif
  366. #endif
  367.  
  368. extern char *sys_siglist[];
  369.  
  370.  
  371. /* Hook for `exec_file_command' command to call.  */
  372.  
  373. extern void (*exec_file_display_hook) ();
  374.    
  375. /* File names of core file and executable file.  */
  376.  
  377. extern char *corefile;
  378. extern char *execfile;
  379.  
  380. /* Descriptors on which core file and executable file are open.
  381.    Note that the execchan is closed when an inferior is created
  382.    and reopened if the inferior dies or is killed.  */
  383.  
  384. extern int corechan;
  385. extern int execchan;
  386.  
  387. /* Last modification time of executable file.
  388.    Also used in source.c to compare against mtime of a source file.  */
  389.  
  390. extern int exec_mtime;
  391.  
  392. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  393.  
  394. extern CORE_ADDR data_start;
  395. extern CORE_ADDR data_end;
  396. extern CORE_ADDR stack_start;
  397. extern CORE_ADDR stack_end;
  398.  
  399. /* Virtual addresses of bounds of two areas of memory in the exec file.
  400.    Note that the data area in the exec file is used only when there is no core file.  */
  401.  
  402. extern CORE_ADDR text_start;
  403. extern CORE_ADDR text_end;
  404.  
  405. extern CORE_ADDR exec_data_start;
  406. extern CORE_ADDR exec_data_end;
  407.  
  408. /* Address in executable file of start of text area data.  */
  409.  
  410. extern int text_offset;
  411.  
  412. /* Address in executable file of start of data area data.  */
  413.  
  414. extern int exec_data_offset;
  415.  
  416. /* Address in core file of start of data area data.  */
  417.  
  418. extern int data_offset;
  419.  
  420. /* Address in core file of start of stack area data.  */
  421.  
  422. extern int stack_offset;
  423.  
  424. /* various coff data structures */
  425.  
  426. extern FILHDR file_hdr;
  427. extern SCNHDR text_hdr;
  428. extern SCNHDR data_hdr;
  429.  
  430. #endif /* not COFF_FORMAT */
  431.  
  432. /* a.out header saved in core file.  */
  433.   
  434. extern AOUTHDR core_aouthdr;
  435.  
  436. /* a.out header of exec file.  */
  437.  
  438. extern AOUTHDR exec_aouthdr;
  439.  
  440. extern void validate_files ();
  441.  
  442. core_file_command (filename, from_tty)
  443.      char *filename;
  444.      int from_tty;
  445. {
  446.   int val;
  447.   extern char registers[];
  448.  
  449.   /* Discard all vestiges of any previous core file
  450.      and mark data and stack spaces as empty.  */
  451.  
  452.   if (corefile)
  453.     free (corefile);
  454.   corefile = 0;
  455.  
  456.   if (corechan >= 0)
  457.     close (corechan);
  458.   corechan = -1;
  459.  
  460.   data_start = 0;
  461.   data_end = 0;
  462.   stack_start = STACK_END_ADDR;
  463.   stack_end = STACK_END_ADDR;
  464.  
  465.   /* Now, if a new core file was specified, open it and digest it.  */
  466.  
  467.   if (filename)
  468.     {
  469.       filename = tilde_expand (filename);
  470.       make_cleanup (free, filename);
  471.       
  472.       if (have_inferior_p ())
  473.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  474.       corechan = open (filename, O_RDONLY, 0);
  475.       if (corechan < 0)
  476.     perror_with_name (filename);
  477.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  478.       {
  479.     struct user u;
  480.  
  481.     unsigned int reg_offset;
  482.  
  483.     val = myread (corechan, &u, sizeof u);
  484.     if (val < 0)
  485.       perror_with_name ("Not a core file: reading upage");
  486.     if (val != sizeof u)
  487.       error ("Not a core file: could only read %d bytes", val);
  488.     data_start = exec_data_start;
  489.  
  490.     data_end = data_start + NBPG * u.u_dsize;
  491.     stack_start = stack_end - NBPG * u.u_ssize;
  492.     data_offset = NBPG * UPAGES;
  493.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  494.  
  495.     /* Some machines put an absolute address in here and some put
  496.        the offset in the upage of the regs.  */
  497.     reg_offset = (int) u.u_ar0;
  498.     if (reg_offset > NBPG * UPAGES)
  499.       reg_offset -= KERNEL_U_ADDR;
  500.  
  501.     /* I don't know where to find this info.
  502.        So, for now, mark it as not available.  */
  503.     N_SET_MAGIC (core_aouthdr, 0);
  504.  
  505.     /* Read the register values out of the core file and store
  506.        them where `read_register' will find them.  */
  507.  
  508.     {
  509.       register int regno;
  510.       char buf[MAX_REGISTER_RAW_SIZE];
  511.  
  512.       *(long*)buf = 0;
  513.       supply_register (regno, buf);
  514.  
  515.       for (regno = 1; regno < NUM_REGS; regno++)
  516.         {
  517.           int offset;
  518.  
  519. #define PCB_OFFSET(FIELD) ((int)&((struct user*)0)->u_pcb.FIELD)
  520.           if (regno < FP0_REGNUM)
  521.           offset =  UPAGES*NBPG-EF_SIZE+4*((regno)+EF_AT-1);
  522.           else if (regno < PC_REGNUM)
  523.           offset = PCB_OFFSET(pcb_fpregs[0]) + 4*(regno-FP0_REGNUM);
  524.           else if (regno == LO_REGNUM)
  525.           offset = UPAGES*NBPG-EF_SIZE+4*EF_MDLO;
  526.           else if (regno == HI_REGNUM)
  527.           offset = UPAGES*NBPG-EF_SIZE+4*EF_MDHI;
  528.           else if (regno == PC_REGNUM)
  529.           offset = UPAGES*NBPG-EF_SIZE+4*EF_EPC;
  530.           else if (regno == CAUSE_REGNUM)
  531.           offset = UPAGES*NBPG-EF_SIZE+4*EF_CAUSE;
  532.           else if (regno == FCRIR_REGNUM)
  533.           offset = PCB_OFFSET(pcb_fpc_eir);
  534.           else if (regno == FCRCS_REGNUM)
  535.           offset = PCB_OFFSET(pcb_fpc_csr);
  536.           else
  537.           continue;
  538.  
  539.           val = lseek (corechan, offset, 0);
  540.           if (val < 0
  541.           || (val = myread (corechan, buf, sizeof buf)) < 0)
  542.         {
  543.           char * buffer = (char *) alloca (strlen (reg_names[regno])
  544.                            + 30);
  545.           strcpy (buffer, "Reading register ");
  546.           strcat (buffer, reg_names[regno]);
  547.                            
  548.           perror_with_name (buffer);
  549.         }
  550.  
  551.           supply_register (regno, buf);
  552.         }
  553.     }
  554.       }
  555.       if (filename[0] == '/')
  556.     corefile = savestring (filename, strlen (filename));
  557.       else
  558.     {
  559.       corefile = concat (current_directory, "/", filename);
  560.     }
  561.  
  562.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  563.                         read_pc ()));
  564.       select_frame (get_current_frame (), 0);
  565.       validate_files ();
  566.     }
  567.   else if (from_tty)
  568.     printf ("No core file now.\n");
  569. }
  570.  
  571. exec_file_command (filename, from_tty)
  572.      char *filename;
  573.      int from_tty;
  574. {
  575.   int val;
  576.  
  577.   /* Eliminate all traces of old exec file.
  578.      Mark text segment as empty.  */
  579.  
  580.   if (execfile)
  581.     free (execfile);
  582.   execfile = 0;
  583.   data_start = 0;
  584.   data_end -= exec_data_start;
  585.   text_start = 0;
  586.   text_end = 0;
  587.   exec_data_start = 0;
  588.   exec_data_end = 0;
  589.   if (execchan >= 0)
  590.     close (execchan);
  591.   execchan = -1;
  592.  
  593.   /* Now open and digest the file the user requested, if any.  */
  594.  
  595.   if (filename)
  596.     {
  597.       filename = tilde_expand (filename);
  598.       make_cleanup (free, filename);
  599.       
  600.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  601.             &execfile);
  602.       if (execchan < 0)
  603.     perror_with_name (filename);
  604.  
  605.       {
  606.     int aout_hdrsize;
  607.     int num_sections;
  608.  
  609.     if (read_file_hdr (execchan, &file_hdr) < 0)
  610.       error ("\"%s\": not in executable format.", execfile);
  611.  
  612.     aout_hdrsize = file_hdr.f_opthdr;
  613.     num_sections = file_hdr.f_nscns;
  614.  
  615.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  616.       error ("\"%s\": can't read optional aouthdr", execfile);
  617.  
  618.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections) < 0)
  619.       error ("\"%s\": can't read text section header", execfile);
  620.  
  621.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections) < 0)
  622.       error ("\"%s\": can't read data section header", execfile);
  623.  
  624.     text_start = exec_aouthdr.text_start;
  625.     text_end = text_start + exec_aouthdr.tsize;
  626.     text_offset = 0; /* text_hdr.s_scnptr */
  627.     exec_data_start = exec_aouthdr.data_start;
  628.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  629.     exec_data_offset = exec_aouthdr.tsize; /*data_hdr.s_scnptr;*/
  630.     data_start = exec_data_start;
  631.     data_end += exec_data_start;
  632.     exec_mtime = file_hdr.f_timdat;
  633.       }
  634.  
  635.       validate_files ();
  636.     }
  637.   else if (from_tty)
  638.     printf ("No exec file now.\n");
  639.  
  640.   /* Tell display code (if any) about the changed file name.  */
  641.   if (exec_file_display_hook)
  642.     (*exec_file_display_hook) (filename);
  643. }
  644.  
  645. #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
  646.  
  647. int
  648. read_next_frame_reg(fi, regno)
  649.      FRAME fi;
  650.      int regno;
  651. {
  652.   for (; fi; fi = fi->next)
  653.       if (regno == SP_REGNUM) return fi->frame;
  654.       else if (fi->saved_regs->regs[regno])
  655.     return read_memory_integer(fi->saved_regs->regs[regno], 4);
  656.   return read_register(regno);
  657. }
  658.  
  659. int
  660. frame_saved_pc(frame)
  661.      FRAME frame;
  662. {
  663.   mips_proc_info *proc_desc = (mips_proc_info*)frame->proc_desc;
  664.   int pcreg = proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM;
  665.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  666.       return read_memory_integer(frame->frame - 4, 4);
  667. #if 0
  668.   /* If in the procedure prologue, RA_REGNUM might not have been saved yet.
  669.    * Assume non-leaf functions start with:
  670.    *    addiu $sp,$sp,-frame_size
  671.    *    sw $ra,ra_offset($sp)
  672.    * This if the pc is pointing at either of these instructions,
  673.    * then $ra hasn't been trashed.
  674.    * If the pc has advanced beyond these two instructions,
  675.    * then $ra has been saved.
  676.    * critical, and much more complex. Handling $ra is enough to get
  677.    * a stack trace, but some register values with be wrong.
  678.    */
  679.   if (frame->proc_desc && frame->pc < PROC_LOW_ADDR(proc_desc) + 8)
  680.       return read_register(pcreg);
  681. #endif
  682.   return read_next_frame_reg(frame, pcreg);
  683. }
  684.  
  685. static PDR temp_proc_desc;
  686. static struct frame_saved_regs temp_saved_regs;
  687.  
  688. CORE_ADDR heuristic_proc_start(pc)
  689.     CORE_ADDR pc;
  690. {
  691.  
  692.     CORE_ADDR start_pc = pc;
  693.     CORE_ADDR fence = start_pc - 10000;
  694.     if (fence < VM_MIN_ADDRESS) fence = VM_MIN_ADDRESS;
  695.     /* search back for previous return */
  696.     for (start_pc -= 4; ; start_pc -= 4)
  697.     if (start_pc < fence) return 0; 
  698.     else if (ABOUT_TO_RETURN(start_pc))
  699.         break;
  700.  
  701.     start_pc += 8; /* skip return, and iys delay slot */
  702. #if 0
  703.     /* skip nops (usually 1) 0 - is this */
  704.     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
  705.     start_pc += 4;
  706. #endif
  707.     return start_pc;
  708. }
  709.  
  710. PDR *heuristic_proc_desc(start_pc, limit_pc, next_frame)
  711.     CORE_ADDR start_pc, limit_pc;
  712.     FRAME next_frame;
  713. {
  714.     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
  715.     CORE_ADDR cur_pc;
  716.     extern int breakpoints_inserted;
  717.     int frame_size;
  718.     int has_frame_reg = 0;
  719.     int reg30; /* Value of $r30. Used by gcc for frame-pointer */
  720.     unsigned long reg_mask = 0;
  721.     if (start_pc == 0) return NULL;
  722.     bzero(&temp_proc_desc, sizeof(PDR));
  723.     bzero(&temp_saved_regs, sizeof(struct frame_saved_regs));
  724.     if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200;
  725.     remove_breakpoints ();
  726.     breakpoints_inserted = 0;
  727.   restart:
  728.     frame_size = 0;
  729.     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
  730.     unsigned long word = read_memory_integer(cur_pc, 4);
  731.     if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
  732.         frame_size += (-word) & 0xFFFF;
  733.     else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
  734.         frame_size += (-word) & 0xFFFF;
  735.     else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
  736.         int reg = (word & 0x001F0000) >> 16;
  737.         reg_mask |= 1 << reg;
  738.         temp_saved_regs.regs[reg] = sp + (short)word;
  739.     }
  740.     else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
  741.         if ((unsigned short)word != frame_size)
  742.         reg30 = sp + (unsigned short)word;
  743.         else if (!has_frame_reg) {
  744.         int alloca_adjust;
  745.         has_frame_reg = 1;
  746.         reg30 = read_next_frame_reg(next_frame, 30);
  747.         alloca_adjust = reg30 - (sp + (unsigned short)word);
  748.         if (alloca_adjust > 0) {
  749.             /* FP > SP + frame_size. This may be because
  750.             /* of an alloca or somethings similar.
  751.              * Fix sp to "pre-alloca" value, and try again.
  752.              */
  753.             sp += alloca_adjust;
  754.             goto restart;
  755.         }
  756.         }
  757.     }
  758.     else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
  759.         int reg = (word & 0x001F0000) >> 16;
  760.         reg_mask |= 1 << reg;
  761.         temp_saved_regs.regs[reg] = reg30 + (short)word;
  762.     }
  763.     }
  764.     if (has_frame_reg) {
  765.     PROC_FRAME_REG(&temp_proc_desc) = 30;
  766.     PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
  767.     }
  768.     else {
  769.     PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
  770.     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
  771.     }
  772.     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
  773.     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
  774.     return &temp_proc_desc;
  775. }
  776.  
  777. mips_proc_info *
  778. find_proc_desc(pc, next_frame)
  779.     CORE_ADDR pc;
  780.     FRAME next_frame;
  781. {
  782.   mips_proc_info *proc_desc;
  783.  
  784.   int lo = 0;
  785.   int hi = proc_desc_length-1;
  786.   if (hi >= 0 && PROC_LOW_ADDR(&proc_desc_table[0]) <= pc
  787.       && PROC_HIGH_ADDR(&proc_desc_table[hi]) > pc)
  788.     {
  789.       for (;;) {
  790.     int new = (lo + hi) >> 1;
  791.     proc_desc = &proc_desc_table[new];
  792.     if (PROC_LOW_ADDR(proc_desc) > pc) hi = new;
  793.     else if (PROC_HIGH_ADDR(proc_desc) <= pc) lo = new;
  794.     else {
  795.         /* IF this is the topmost frame AND
  796.          * (this proc does not have debugging information OR
  797.          * the PC is in the procedure prologue)
  798.          * THEN create a "hueristic" proc_desc (by analyzing
  799.          * the actual code) to replace the "official" proc_desc.
  800.          */
  801.         if (next_frame == NULL) {
  802.         struct symtab_and_line val;
  803.         struct symbol *proc_symbol = PROC_SYMBOL(proc_desc);
  804.         if (proc_symbol) {
  805.             val = find_pc_line (BLOCK_START
  806.                       (SYMBOL_BLOCK_VALUE(proc_symbol)),
  807.                       0);
  808.             val.pc = val.end ? val.end : pc;
  809.         }
  810.         if (!proc_symbol || pc < val.pc) {
  811.             mips_proc_info *found_heuristic =
  812.             heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
  813.                         pc, next_frame);
  814.             if (found_heuristic) proc_desc = found_heuristic;
  815.         }
  816.         }
  817.         break;
  818.     }
  819.     if (hi-lo <= 1) { proc_desc = 0; break; }
  820.       }
  821.     }
  822.   else
  823.     {
  824.       register struct linked_proc_info *link;
  825.       for (link = linked_proc_desc_table; link; link = link->next)
  826.       if (PROC_LOW_ADDR(&link->info) <= pc
  827.           && PROC_HIGH_ADDR(&link->info) > pc)
  828.           return &link->info;
  829.       proc_desc =
  830.       heuristic_proc_desc(heuristic_proc_start(pc), pc, next_frame);
  831.     }
  832.   return proc_desc;
  833. }
  834.  
  835. PDR *cached_proc_desc;
  836.  
  837. FRAME_ADDR frame_chain(frame)
  838.     FRAME frame;
  839. {
  840.     extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  841.     mips_proc_info *proc_desc;
  842.     CORE_ADDR saved_pc = frame_saved_pc(frame);
  843.     if (startup_file_start)
  844.       { /* has at least the __start symbol */
  845.     if (saved_pc == 0 || !outside_startup_file (saved_pc)) return 0;
  846.       }
  847.     else
  848.       { /* This hack depends on the internals of __start. */
  849.     /* We also assume the breakpoints are *not* inserted */
  850.         if (read_memory_integer (saved_pc + 8, 4) == 0xd) /* break */
  851.         return 0;
  852.       }
  853.     proc_desc = find_proc_desc(saved_pc, frame);
  854.     if (!proc_desc) return 0;
  855.     cached_proc_desc = proc_desc;
  856.     return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
  857.     + PROC_FRAME_OFFSET(proc_desc);
  858. }
  859.  
  860. void
  861. init_extra_frame_info(fci)
  862.      struct frame_info *fci;
  863. {
  864.   extern struct obstack frame_cache_obstack;
  865.   /* Use proc_desc calculated in frame_chain */
  866.   PDR *proc_desc = fci->next ? cached_proc_desc :
  867.       find_proc_desc(fci->pc, fci->next);
  868.   fci->saved_regs = (struct frame_saved_regs*)
  869.     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
  870.   bzero(fci->saved_regs, sizeof(struct frame_saved_regs));
  871.   fci->proc_desc =
  872.       proc_desc == &temp_proc_desc ? (char*)NULL : (char*)proc_desc;
  873.   if (proc_desc)
  874.     {
  875.       int ireg;
  876.       CORE_ADDR reg_position;
  877.       unsigned long mask;
  878.       /* r0 bit means kernel trap */
  879.       int kernel_trap = PROC_REG_MASK(proc_desc) & 1;
  880.  
  881.       /* Fixup frame-pointer - only needed for top frame */
  882.       /* This may not be quite right, if procedure has a real frame register */
  883.       if (fci->pc == PROC_LOW_ADDR(proc_desc))
  884.       fci->frame = read_register (SP_REGNUM);
  885.       else
  886.       fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
  887.           + PROC_FRAME_OFFSET(proc_desc);
  888.  
  889.       if (proc_desc == &temp_proc_desc)
  890.       *fci->saved_regs = temp_saved_regs;
  891.       else
  892.       {
  893.       /* find which general-purpose registers were saved */
  894.       reg_position = fci->frame + PROC_REG_OFFSET(proc_desc);
  895.       mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
  896.       for (ireg= 31; mask; --ireg, mask <<= 1)
  897.           if (mask & 0x80000000)
  898.           {
  899.           fci->saved_regs->regs[ireg] = reg_position;
  900.           reg_position -= 4;
  901.           }
  902.       /* find which floating-point registers were saved */
  903.       reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc);
  904.       /* The freg_offset points to where the first *double* register is saved.
  905.        * So skip to the high-order word. */
  906.       reg_position += 4;
  907.       mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
  908.       for (ireg = 31; mask; --ireg, mask <<= 1)
  909.           if (mask & 0x80000000)
  910.           {
  911.           fci->saved_regs->regs[32+ireg] = reg_position;
  912.           reg_position -= 4;
  913.           }
  914.       }
  915.  
  916.       /* hack: if argument regs are saved, guess these contain args */
  917.       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
  918.       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
  919.       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
  920.       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
  921.       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
  922.  
  923.       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  924.     }
  925.   if (fci->next == 0)
  926.       supply_register(FP_REGNUM, &fci->frame);
  927. }
  928.  
  929.  
  930. CORE_ADDR mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
  931.   int nargs;
  932.   value *args;
  933.   CORE_ADDR sp;
  934.   int struct_return;
  935.   CORE_ADDR struct_addr;
  936. {
  937.   CORE_ADDR buf;
  938.   register i;
  939.   int accumulate_size = struct_return ? 4 : 0;
  940.   struct mips_arg { char *contents; int len; int offset; };
  941.   struct mips_arg *mips_args =
  942.       (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg));
  943.   register struct mips_arg *m_arg;
  944.   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
  945.     extern value value_arg_coerce();
  946.     value arg = value_arg_coerce (args[i]);
  947.     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
  948.     /* This entire mips-specific routine is because doubles must be aligned
  949.      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
  950.      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
  951.      * breaks their varargs implementation...). A correct solution
  952.      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
  953.      * in stdarg.h/varargs.h).
  954.      */
  955.     if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8;
  956.     m_arg->offset = accumulate_size;
  957.     accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
  958.     m_arg->contents = VALUE_CONTENTS(arg);
  959.   }
  960.   accumulate_size = (accumulate_size + 7) & (-8);
  961.   if (accumulate_size < 16) accumulate_size = 16; 
  962.   sp -= accumulate_size;
  963.   for (i = nargs; m_arg--, --i >= 0; )
  964.     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
  965.   if (struct_return) {
  966.     buf = struct_addr;
  967.     write_memory(sp, &buf, sizeof(CORE_ADDR));
  968. }
  969.   return sp;
  970. }
  971.  
  972. /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
  973. #define MASK(i,j) ((1 << (j)+1)-1 ^ (1 << (i))-1)
  974.  
  975. void
  976. push_dummy_frame()
  977. {
  978.   int ireg;
  979.   struct linked_proc_info *link = (struct linked_proc_info*)
  980.       xmalloc(sizeof(struct linked_proc_info));
  981.   mips_proc_info *proc_desc = &link->info;
  982.   CORE_ADDR sp = read_register (SP_REGNUM);
  983.   CORE_ADDR save_address;
  984.   REGISTER_TYPE buffer;
  985.   link->next = linked_proc_desc_table;
  986.   linked_proc_desc_table = link;
  987. #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
  988. #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
  989. #define GEN_REG_SAVE_COUNT 22
  990. #define FLOAT_REG_SAVE_MASK MASK(0,19)
  991. #define FLOAT_REG_SAVE_COUNT 20
  992. #define SPECIAL_REG_SAVE_COUNT 4
  993.   /*
  994.    * The registers we must save are all those not preserved across
  995.    * procedure calls. Dest_Reg (see m-mips.h) must also be saved.
  996.    * In addition, we must save the PC, and PUSH_FP_REGNUM.
  997.    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
  998.    *
  999.    * Dummy frame layout:
  1000.    *  (high memory)
  1001.    *     Saved PC
  1002.    *    Saved MMHI, MMLO, FPC_CSR
  1003.    *    Saved R31
  1004.    *    Saved R28
  1005.    *    ...
  1006.    *    Saved R1
  1007.    *    Saved D18 (i.e. F19, F18)
  1008.    *    ...
  1009.    *    Saved D0 (i.e. F1, F0)
  1010.    *    CALL_DUMMY (subroutine stub; see m-mips.h)
  1011.    *    Parameter build area (not yet implemented)
  1012.    *  (low memory)
  1013.    */
  1014.   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
  1015.   PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
  1016.   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
  1017.       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
  1018.   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
  1019.       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
  1020.   /* save general registers */
  1021.   save_address = sp + PROC_REG_OFFSET(proc_desc);
  1022.   for (ireg = 32; --ireg >= 0; )
  1023.     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
  1024.       {
  1025.     buffer = read_register (ireg);
  1026.     write_memory (save_address, &buffer, sizeof(REGISTER_TYPE));
  1027.     save_address -= 4;
  1028.       }
  1029.   /* save floating-points registers */
  1030.   save_address = sp + PROC_FREG_OFFSET(proc_desc);
  1031.   for (ireg = 32; --ireg >= 0; )
  1032.     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
  1033.       {
  1034.     buffer = read_register (ireg);
  1035.     write_memory (save_address, &buffer, 4);
  1036.     save_address -= 4;
  1037.       }
  1038.   write_register (PUSH_FP_REGNUM, sp);
  1039.   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
  1040.   PROC_FRAME_OFFSET(proc_desc) = 0;
  1041.   buffer = read_register (PC_REGNUM);
  1042.   write_memory (sp - 4, &buffer, sizeof(REGISTER_TYPE));
  1043.   buffer = read_register (HI_REGNUM);
  1044.   write_memory (sp - 8, &buffer, sizeof(REGISTER_TYPE));
  1045.   buffer = read_register (LO_REGNUM);
  1046.   write_memory (sp - 12, &buffer, sizeof(REGISTER_TYPE));
  1047.   buffer = read_register (FCRCS_REGNUM);
  1048.   write_memory (sp - 16, &buffer, sizeof(REGISTER_TYPE));
  1049.   sp -= 4 * (GEN_REG_SAVE_COUNT+FLOAT_REG_SAVE_COUNT+SPECIAL_REG_SAVE_COUNT);
  1050.   write_register (SP_REGNUM, sp);
  1051.   PROC_HIGH_ADDR(proc_desc) = sp;
  1052.   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE;
  1053.   SET_PROC_DESC_IS_DUMMY(proc_desc);
  1054.   PROC_PC_REG(proc_desc) = RA_REGNUM;
  1055. }
  1056.  
  1057. void
  1058. pop_frame()
  1059. { register int regnum;
  1060.   FRAME frame = get_current_frame ();
  1061.   CORE_ADDR new_sp = frame->frame;
  1062.   mips_proc_info *proc_desc = (PDR*)frame->proc_desc;
  1063.   if (PROC_DESC_IS_DUMMY(proc_desc))
  1064.     {
  1065.       struct linked_proc_info **ptr = &linked_proc_desc_table;;
  1066.       for (; &ptr[0]->info != proc_desc; ptr = &ptr[0]->next )
  1067.       if (ptr[0] == NULL) abort();
  1068.       *ptr = ptr[0]->next;
  1069.       free (ptr[0]);
  1070.       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
  1071.       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
  1072.       write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
  1073.     }
  1074.   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
  1075.   if (frame->proc_desc) {
  1076.     for (regnum = 32; --regnum >= 0; )
  1077.       if (PROC_REG_MASK(proc_desc) & (1 << regnum))
  1078.     write_register (regnum,
  1079.           read_memory_integer (frame->saved_regs->regs[regnum], 4));
  1080.     for (regnum = 64; --regnum >= 32; )
  1081.       if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
  1082.     write_register (regnum,
  1083.           read_memory_integer (frame->saved_regs->regs[regnum], 4));
  1084.   }
  1085.   write_register (SP_REGNUM, new_sp);
  1086.   flush_cached_frames ();
  1087.   set_current_frame (create_new_frame (new_sp, read_pc ()));
  1088. }
  1089.  
  1090. static mips_print_register(regnum, all)
  1091.      int regnum, all;
  1092. {
  1093.       unsigned char raw_buffer[8];
  1094.       REGISTER_TYPE val;
  1095.  
  1096.       read_relative_register_raw_bytes (regnum, raw_buffer);
  1097.  
  1098.       if (!(regnum & 1) && regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32) {
  1099.       read_relative_register_raw_bytes (regnum+1, raw_buffer+4);
  1100.       printf_filtered ("(d%d: ", regnum&31);
  1101.       val_print (builtin_type_double, raw_buffer, 0,
  1102.              stdout, 0, 1, 0, Val_pretty_default);
  1103.       printf_filtered ("); ", regnum&31);
  1104.       }
  1105.       fputs_filtered (reg_names[regnum], stdout);
  1106. #ifndef NUMERIC_REG_NAMES
  1107.       if (regnum < 32)
  1108.       printf_filtered ("(r%d): ", regnum);
  1109.       else
  1110. #endif
  1111.       printf_filtered (": ");
  1112.  
  1113.       /* If virtual format is floating, print it that way.  */
  1114.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
  1115.       && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) {
  1116.       val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
  1117.              stdout, 0, 1, 0, Val_pretty_default);
  1118.       }
  1119.       /* Else print as integer in hex.  */
  1120.       else
  1121.     {
  1122.       long val;
  1123.  
  1124.       bcopy (raw_buffer, &val, sizeof (long));
  1125.       if (val == 0)
  1126.         printf_filtered ("0");
  1127.       else if (all)
  1128.         printf_filtered ("0x%x", val);
  1129.       else
  1130.         printf_filtered ("0x%x=%d", val, val);
  1131.     }
  1132. }
  1133.  
  1134. mips_do_registers_info(regnum)
  1135.      int regnum;
  1136. {
  1137.   if (regnum != -1) {
  1138.       mips_print_register (regnum, 0);
  1139.       printf_filtered ("\n");
  1140.   }
  1141.   else {
  1142.       for (regnum = 0; regnum < NUM_REGS; ) {
  1143.       mips_print_register (regnum, 1);
  1144.       regnum++;
  1145.       if ((regnum & 3) == 0 || regnum == NUM_REGS)
  1146.           printf_filtered (";\n");
  1147.       else
  1148.           printf_filtered ("; ");
  1149.       }
  1150.   }
  1151. }
  1152.